//
//  CFProgressHUDFadeZoomAnimation.m
//  CFProgressHUD
//
//  Created by 千域互联 on 2018/3/18.
//  Copyright © 2018年 千域互联. All rights reserved.
//

#import "CFProgressHUDFadeZoomAnimation.h"
#import "CFProgressHUD.h"

@implementation CFProgressHUDFadeZoomAnimation

- (instancetype)init {
    if (self = [super init]) {
        self.shrinkAnimationDuaration = 0.2;
        self.expandAnimationDuaration = 0.1;
        self.expandScale = CGSizeMake(1.1f, 1.1f);
    }
    return self;
}

- (void)show
{
    [super show];
    
    self.progressHUD.alpha = .0f;
    self.progressHUD.HUDView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
    
    NSTimeInterval totalDuration = self.expandAnimationDuaration+self.shrinkAnimationDuaration;
    
    self.progressHUD.hidden = NO;
    
    [UIView animateWithDuration:totalDuration delay:.0 options:(UIViewAnimationOptions)(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.progressHUD.alpha = 1.0f;
    } completion:nil];
    
    [UIView animateWithDuration:self.shrinkAnimationDuaration delay:.0f options:(UIViewAnimationOptions)(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        self.progressHUD.HUDView.transform = CGAffineTransformMakeScale(self.expandScale.width, self.expandScale.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:self.expandAnimationDuaration delay:.0f options:(UIViewAnimationOptions)(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState) animations:^{
            self.progressHUD.HUDView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [self animationFinished];
        }];
    }];
}

- (void)hide
{
    [super hide];
    
    NSTimeInterval totalDuration = self.expandAnimationDuaration+self.shrinkAnimationDuaration;
    
    [UIView animateWithDuration:totalDuration delay:.0f options:(UIViewAnimationOptions)(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.progressHUD.alpha = .0f;
    } completion:nil];
    
    [UIView animateWithDuration:self.expandAnimationDuaration delay:.0f options:(UIViewAnimationOptions)(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        self.progressHUD.HUDView.transform = CGAffineTransformMakeScale(self.expandScale.width, self.expandScale.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:self.shrinkAnimationDuaration delay:.0f options:(UIViewAnimationOptions)(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState) animations:^{
            self.progressHUD.HUDView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
        } completion:^(BOOL finished) {
            self.progressHUD.HUDView.transform = CGAffineTransformIdentity;
            [self animationFinished];
        }];
    }];
}

@end
